package com.ksd.quartz.util;

import com.ksd.common.config.PlayControlConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Author y.wu
 * @Create 2024/8/9 下午 4:16
 */
public class XshellTool {

    private static final Logger logger = LoggerFactory.getLogger(XshellTool.class);

    public static Integer printExet(String scriptPath){
        // 写入脚本文件
//        XshellTool.setEditDecryptText(scriptPath,code);

        // 修改脚本文件权限
        try {
//            Process process = Runtime.getRuntime().exec(new String[]{"chmod", "u+x", scriptPath});
            Process process = Runtime.getRuntime().exec(new String[]{"chmod", "777", scriptPath});
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        Integer result = 0;
        // 执行脚本并获取输出
        try {
            Process process = Runtime.getRuntime().exec(scriptPath);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
                System.out.println(line);
            }

            result =  process.waitFor();
            System.out.println("Exited with code: " + result);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return result;
        // 删除脚本文件（可选）
//        File file = new File(scriptPath);
//        file.delete();
    }

    public static String getPidValue(String fileName){
        // 指定要读取的文件的路径
//        String filePath = "/home/udrm/wuyun_drm_decrypt/drm_pid.txt";
        String filePath = PlayControlConfig.getXshellPath()+fileName;

        // 使用File对象表示该文件
        File file = new File(filePath);

        // 确保文件存在
        if (file.exists()) {
            try {
                // 创建FileReader和BufferedReader来读取文件内容
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                String line;
                String temp = "";
                // 逐行读取文件内容
                while ((line = bufferedReader.readLine()) != null) {
                    logger.info("读取到的进程数据:"+line);
                    temp = line;
                }

                // 关闭BufferedReader和FileReader
                bufferedReader.close();
                fileReader.close();
                return temp;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件不存在！‌");
        }
        return null;
    }
    public static void setEditDecryptText(String filePath,String decryptStr) {
        try {
            File file = new File(filePath);
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempString = null;

                CharArrayWriter caw = new CharArrayWriter();
                int line = 1;
                while ((tempString = reader.readLine()) != null) {
//                    logger.info("读取到的数据为: "+tempString);
                    if (tempString.startsWith("nohup ")) {
//                        String replaceAll = tempString.replaceAll(tempString,Pattern.quote(decryptStr));
                        String replaceAll = decryptStr;
                        caw.write(replaceAll);
                        caw.append(System.getProperty("line.separator"));
                        logger.info("更新数据为: "+replaceAll);
                    } else {
                        caw.write(tempString);
                        caw.append(System.getProperty("line.separator"));
                    }
                    line++;
                }
                reader.close();

                file.setWritable(true, true);
                FileWriter fw = new FileWriter(file);
                caw.writeTo(fw);
                fw.close();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void setEditCheckFfmpegText(String filePath,String decryptStr) {
        try {
            File file = new File(filePath);
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempString = null;

                CharArrayWriter caw = new CharArrayWriter();
                int line = 1;
                while ((tempString = reader.readLine()) != null) {
//                    logger.info("读取到的数据为: "+tempString);
                    if (tempString.startsWith("checkTaskLe=")) {
//                        String replaceAll = tempString.replaceAll(tempString,Pattern.quote(decryptStr));
                        String replaceAll = decryptStr;
                        caw.write(replaceAll);
                        caw.append(System.getProperty("line.separator"));
                        logger.info("更新数据为: "+replaceAll);
                    } else {
                        caw.write(tempString);
                        caw.append(System.getProperty("line.separator"));
                    }
                    line++;
                }
                reader.close();

                file.setWritable(true, true);
                FileWriter fw = new FileWriter(file);
                caw.writeTo(fw);
                fw.close();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void setEditDecryptPid(String filePath,String decryptStr) {
        try {
            File file = new File(filePath);
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempString = null;

                CharArrayWriter caw = new CharArrayWriter();
                int line = 1;
                while ((tempString = reader.readLine()) != null) {
//                    logger.info("读取到的数据为: "+tempString);
                    if (tempString.startsWith("kill ")) {
                        String replaceAll = decryptStr;
                        caw.write(replaceAll);
                        caw.append(System.getProperty("line.separator"));
                        logger.info("更新数据为: "+replaceAll);
                    } else {
                        caw.write(tempString);
                        caw.append(System.getProperty("line.separator"));
                    }
                    line++;
                }
                reader.close();

                file.setWritable(true, true);
                FileWriter fw = new FileWriter(file);
                caw.writeTo(fw);
                fw.close();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
